// import { SelectCreateComponent } from '../../components/select-create/select-create.component'
import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core'
import { FormBuilder, Validators, FormGroup } from '@angular/forms'
import { QlMessageService } from 'qloud-angular'
import { plainToClass } from 'class-transformer'
import { DictService } from '@app/shared/utils/dict.service'
import { ApiService } from '../../services/api.service'
import { ActivatedRoute } from '@angular/router'
import { ModalService } from '@app/shared/utils'
import { NetService, PageService } from '@app/core/http'
import { Router } from '@angular/router'
import { CursorService } from '@app/core/http/cursor.service'
import { Store } from '@ngxs/store'
import { ModalConfirmComponent } from '@app/shared/components/modal-confirm/modal-confirm.component'

@Component({
  selector: 'app-customer-select',
  templateUrl: './customer-select.component.html',
  styleUrls: ['./customer-select.component.css'],
  providers: [ModalService]
})
export class CustomerSelectComponent implements OnInit {
  public applyList: any[] = []
  public formGroup: FormGroup
  public editformGroup:FormGroup
  public receiverNum

  @ViewChild('addCustomerapply', { static: true })
  private addCustomerapplyTemp: TemplateRef<any>

  @ViewChild('editCustomerapply', { static: true })
  private editCustomerapplyTemp: TemplateRef<any>

  constructor(
    private fb: FormBuilder,
    private apiService: ApiService,
    public modal: ModalService,
    public dictService: DictService,
    public cursorService: CursorService,
    private message: QlMessageService,
    public pageService: PageService,
    private router: Router,
    private store: Store,
    public route: ActivatedRoute
  ) {
    this.formGroup = this.fb.group({
      receiverName: [],
      receiverOrgName: [],
      receiveTime: [],
      reason: []
    })

    this.editformGroup = this.fb.group({
      receiverName: [],
      receiverOrgName: [],
      receiveTime: [],
      reason: []
    })

  }

  ngOnInit() {
    this.onRefresh()
  }

  public onRefresh() {
    this.queryList()
  }

  public queryList() {
    this.apiService.getCustomerSelectList(this.cursorService).subscribe(data => {
      this.applyList = data
    })
  }

  public onCreate() {
    this.modal
      .open({
        title: '客户申领',
        component: this.addCustomerapplyTemp
      })
      .subscribe(() => {})
  }

  public submitApply() {
    const param = {
      receiverName: this.formGroup.value.receiverName,
      receiverOrgName: this.formGroup.value.receiverOrgName,
      receiveTime: this.formGroup.value.receiveTime,
      reason: this.formGroup.value.reason
    }
    this.apiService.customerApply(param).subscribe(data => {
      // this.message.success(data[0].message)
      this.queryList()
      this.modal.close()
    })
  }

  public delApply(ref: any) {
    this.modal
      .open({
        title: '确认删除',
        component: ModalConfirmComponent
      })
      .subscribe(() => {
        const receiverno = ref.rowData.receiverNo
        this.apiService.deleteApply(receiverno).subscribe(data => {
          // this.message.success(data[0].message)
          this.queryList()
        })
      })
  }

  /**
   * 查询一条客户申领
   */
  public editApply(ref: any){
    this.modal
    .open({
      title: '编辑客户申领',
      component: this.editCustomerapplyTemp
    })
    .subscribe(() => {})
    this.receiverNum = ref.rowData.receiverNo
    this.apiService.lookApply(this.receiverNum).subscribe(data => {
      this.editformGroup.patchValue(data)
    })
  }

  /**
   * 提交编辑客户申领
   */
  public editSumit(){
    const param = {
      receiverName: this.editformGroup.value.receiverName,
      receiverOrgName: this.editformGroup.value.receiverOrgName,
      receiveTime: this.editformGroup.value.receiveTime,
      reason: this.editformGroup.value.reason
    }
    this.apiService.editApply(param,this.receiverNum).subscribe(data => {

    })
  }
}
